package vg.modules.search.components.firstPanel;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JComponent;

/**
 * This class realizes comboxBox for graphs and views.
 * @author tzolotuhin
 */
public class GraphComboBox {
	public static final int DEF_GRAPH = 1;
	public static final int DEF_VIEW = 2;
	//-------------------------------------------
	private final JComboBox comboBox;
	private final List<Integer>types;
	private final List<Object>objects;
	private final List<String>titles;
	/**
	 * Constructor.
	 */
	public GraphComboBox() {
		this.comboBox = new JComboBox();
		this.types = new ArrayList<Integer>();
		this.objects = new ArrayList<Object>();
		this.titles = new ArrayList<String>();
	}
	public synchronized void addItem(int what, Object obj, String title) {
		this.types.add(what);
		this.objects.add(obj);
		this.titles.add(title);
		this.comboBox.addItem(title);
	}
	public synchronized void removeItem(int index) {
		this.types.remove(index);
		this.objects.remove(index);
		this.titles.remove(index);
		this.comboBox.removeItemAt(index);		
	}
	public synchronized int getItemCount() {
		return(this.objects.size());
	}
	public synchronized int getSelectedType() {
		int index = this.comboBox.getSelectedIndex();
		if(index >= 0 && index < this.types.size()) {
			return(this.types.get(index));
		}
		return(-1);
	}
	public synchronized Object getSelectedObject() {
		int index = this.comboBox.getSelectedIndex();
		if(index >= 0 && index < this.types.size()) {
			return(this.objects.get(index));
		}
		return(null);		
	}
	public synchronized void addActionListener(ActionListener listener) {
		this.comboBox.addActionListener(listener);
	}
	public JComponent getView() {
		return(this.comboBox);
	}
}
